import { Component, Input, OnInit } from '@angular/core';
import { myValidNull } from '@core/util/string-utils';
import { MyModalHelper } from '@core/my-modal.helper';
import { NzMessageService } from 'ng-zorro-antd';
import { OtcConvertService } from '@core/routes-services/otc-convert.service';
import { AuditingComponent } from '../../auditing/auditing.component';
import { OtcConvertViewComponent } from './otc-convert-view/otc-convert-view.component';
import { OTC_CONVERT_CASH_STATUS_OPTION, OTC_CONVERT_STATUS_OPTION } from '../../../constant/dictionary';
import { OrderLogisticsComponent } from '../../order-logistics/order-logistics.component';

@Component({
  selector: 'otc-convert-list',
  templateUrl: './otc-convert-list.component.html',
})
export class OtcConvertListComponent implements OnInit {

  isLoading = false;
  keyword: any;
  status: any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };
  statusOptions: any [] = OTC_CONVERT_STATUS_OPTION;
  cashStatusOptions: any [] = OTC_CONVERT_CASH_STATUS_OPTION;

  @Input() type: any;

  constructor(private message: NzMessageService,
              private otcConvertService: OtcConvertService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initPage();
  }

  initPage() {
    this.isLoading = true;
    this.otcConvertService.pagination(this.page, this.keyword, this.status, this.type).then((res) => {
      console.log(res);
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  editEntity = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(OtcConvertViewComponent, { entity: bak }, 800, {
      nzTitle: '兑换信息',
    }).subscribe(result => {
      if (result) {
        temp ? this.search() : this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };

  reject = (entity) => {
    let temp: boolean;
    const bak = {};
    Object.assign(bak, entity);
    bak['remark'] = bak['rejectReason'];
    this.modalHelper.open(AuditingComponent, { entity: bak, businessService: this.otcConvertService }, 360, {
      nzTitle: '驳回兑换请求',
    }).subscribe(result => {
      if (result) {
        this.message.success('驳回成功');
        this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };

  agree = (entity) => {
    this.otcConvertService.auditing(entity.id, { status: 3 }).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.message.success('操作成功');
        this.changePageNo(this.page.number);
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  };
  /**
   * 补发
   * @param entity
   */

    //是否在重复中，防止重复发放
  reSending: boolean = false;
  reSend = (entity) => {
    if (!this.reSending) {
      this.reSending = true;
      this.otcConvertService.reSendCash(entity.id, {}).then((res) => {
        if (res.status.toString().startsWith('20')) {
          this.message.success('补发成功');
          this.changePageNo(this.page.number);
        } else {
          this.message.warning(res.message);
        }
        this.reSending = false;
      }, error => {
        this.message.error(error.message ? error.message : error.error);
        this.reSending = false;
      });
    }
  };
  logistics = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(OrderLogisticsComponent, { entity: bak, businessService: this.otcConvertService }, 520, {
      nzTitle: '物流信息',
    }).subscribe(result => {
      if (result) {
        temp ? this.search() : this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };

}
